package tree;

/** 二叉树 */
public class BinaryTreeDemo {

  public static void main(String[] args) {
    BinaryTree binaryTree = new BinaryTree();
    Student s1 = new Student(1,"wws");
    Student s2 = new Student(2,"grg");
    Student s3 = new Student(3,"vfr");
    Student s4 = new Student(4,"bgf");
    Student s5 = new Student(5,"mnhg");

    s1.setLeft(s2);
    s1.setRight(s3);
    s3.setRight(s4);
    s3.setLeft(s5);
    binaryTree.root=s1;
    /*binaryTree.perOrder();
    System.out.println();
    binaryTree.midOrder();
    System.out.println();
    binaryTree.postOrder();*/
    /*Student res1 = binaryTree.postSearcher(13);
    if (res1!=null){
      System.out.println(res1);
    }else{
      System.out.println("未查到");
    }*/


    binaryTree.perOrder();
    binaryTree.delete(5);
    System.out.println("删除后");
    binaryTree.perOrder();
  }
}

class BinaryTree {
  public Student root;

  // 前序遍历
  public void perOrder() {
    this.root.perOrder();
  }

  // 中序遍历
  public void midOrder() {
    this.root.midOrder();
  }

  // 后序遍历
  public void postOrder() {
    this.root.postOrder();
  }

  //前序查找
  public Student perSearcher(int id) {
    Student result=null;
    if (this.root!=null){
      result= this.root.perSearcher(id);
    }
    return result;
  }
  //中序查找
  public Student midSearcher(int id) {
    Student result=null;
    if (this.root!=null){
      result= this.root.midSearcher(id);
    }
    return result;
  }
  //后序查找
  public Student postSearcher(int id) {
    Student result=null;
    if (this.root!=null){
      result= this.root.postSearcher(id);
    }
    return result;
  }

  //删除结点
  public void delete(int id) {
    if (root==null){
      System.out.println("数空");
    }else{
      if (root.getId()==id){
        root=null;
      }else {
        root.delete(id);
      }
    }
  }
}

class Student {
  private int id;
  private String name;
  private Student left;
  private Student right;

  public Student(int id, String name) {
    this.id = id;
    this.name = name;
  }

  @Override
  public String toString() {
    return "Student{" + "id=" + id + ", name=" + name + '}';
  }

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Student getLeft() {
    return left;
  }

  public void setLeft(Student left) {
    this.left = left;
  }

  public Student getRight() {
    return right;
  }

  public void setRight(Student right) {
    this.right = right;
  }

  // 前序遍历
  public void perOrder() {
    System.out.println(this);
    if (this.left != null) {
      this.left.perOrder();
    }
    if (this.right != null) {
      this.right.perOrder();
    }
  }
  // 中序遍历
  public void midOrder() {

    if (this.left != null) {
      this.left.midOrder();
    }
    System.out.println(this);
    if (this.right != null) {
      this.right.midOrder();
    }
  }
  // 后序遍历
  public void postOrder() {

    if (this.left != null) {
      this.left.postOrder();
    }
    if (this.right != null) {
      this.right.postOrder();
    }
    System.out.println(this);
  }

  //前序查找
  public Student perSearcher(int no) {

    if (this.id==no){
      return this;
    }
    Student result=null;
    if (this.left != null) {
      result=this.left.perSearcher(no);
    }
    if (this.right != null) {
      result=this.right.perSearcher(no);
    }
    return result;
  }
  //中序查找
  public Student midSearcher(int no) {
    Student result=null;
    if (this.left != null) {
      result=this.left.midSearcher(no);
    }
    if (this.id==no){
      return this;
    }
    if (this.right != null) {
      result=this.right.midSearcher(no);
    }
    return result;
  }
  //后序查找
  public Student postSearcher(int no) {
    Student result=null;
    if (this.left != null) {
      result=this.left.postSearcher(no);
    }
    if (this.right != null) {
      result=this.right.postSearcher(no);
    }
    if (this.id==no){
      return this;
    }
    return result;
  }

  public void delete(int no){
    if (this.left != null&&this.left.id==no) {
      this.left=null;
      return;
    }
    if (this.right != null&&this.right.id==no) {
      this.right=null;
      return;
    }
    if (this.left!=null){
      this.left.delete(no);
    }
    if (this.right!=null){
      this.right.delete(no);
    }
  }



}
